package com.fadadianqi.logistics.search.web;

import com.fadadianqi.logistics.search.CustomerIndexService;
import com.fadadianqi.logistics.search.GoodsIndexService;
import com.fadadianqi.logistics.search.SellIndexService;
import com.fadadianqi.logistics.search.VillageIndexService;
import com.yeqiangwei.commons.util.ParamUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;


@Controller("search_indexController")
public class IndexBuildController {

    private static final Log log = LogFactory.getLog(IndexBuildController.class);

    @RequestMapping(value = "/a/search/index", method = RequestMethod.GET)
    public String show(HttpServletRequest request) throws Exception {
        return "/admin/search/index";
    }

    @RequestMapping(value = "/a/search/index", method = RequestMethod.POST)
    public String index(HttpServletRequest request, ModelMap modelMap) throws Exception {

        String build = ParamUtils.getString(request, "build", null);
        boolean customer_index = ParamUtils.getBoolean(request, "customer", false);
        boolean sell_index = ParamUtils.getBoolean(request, "sell", false);
        boolean village_index = ParamUtils.getBoolean(request, "village", false);
        boolean goods_index = ParamUtils.getBoolean(request, "goods", false);

        if (build == null || build.equalsIgnoreCase("build")) {

            if (customer_index) {
                log.info("Member index build...");
                customerIndexService.build();
            }

            if (sell_index) {
                log.info("Sell index build...");
                sellIndexService.build();
            }

            if (village_index) {
                log.info("Village index build...");
                villageIndexService.build();
            }

            if (goods_index) {
                log.info("Goods index build...");
                goodsIndexService.build();
            }
        } else {

            if (customer_index) {
                log.info("Member index rebuild...");
                customerIndexService.rebuild();
            }

            if (sell_index) {
                log.info("Sell index rebuild...");
                sellIndexService.rebuild();
            }

            if (village_index) {
                log.info("Village index rebuild...");
                villageIndexService.rebuild();
            }

            if (goods_index) {
                log.info("Goods index rebuild...");
                goodsIndexService.rebuild();
            }
        }
        log.info("Search index built!!!");
        modelMap.put("remind", "success");
        return "/admin/search/index";
    }

    @Autowired
    private CustomerIndexService customerIndexService;

    @Autowired
    private SellIndexService sellIndexService;

    @Autowired
    private VillageIndexService villageIndexService;

    @Autowired
    private GoodsIndexService goodsIndexService;
}
